
/*
 * GET home page.
 */
var fs = require('fs');/*文件处理类*/
var url = require("url");/*url工具类*/
var qs = require('querystring');/*将字符串转换为json串*/
var DoMysql = require('../sql/domysql');
var logger = require('../util/log.js');

//保存用户信息
exports.saveUserInfo = function(req, res){
  	logger.logger('normal').info("保存用户信息");
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;				
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.saveUserInfo(datainfo.userid,datainfo.username,datainfo.sex,datainfo.birthday,datainfo.phone,datainfo.email,function(results){			
			res.end(results);
		});		
	});
};
//获取用户信息
exports.getUserInfo = function(req, res){

	logger.logger('normal').info("获取用户信息");
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;				
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.getUserInfo(datainfo.userid,function(results){			
			res.end(results);
		});		
	});
};
//打开信息修改页面
exports.personInformation = function(req, res){
  	
	logger.logger('normal').info("打开信息修改页面");
	// 异步读取
	fs.readFile('views/personalInformation.html', function (err, data) {
	   if (err) {
		   return console.error(err);
	   }
	   res.writeHeader(200,{"Content-Type":"text/html"});
	   res.end(data);
	});
};
//打开登录页面
exports.index = function(req, res){
	logger.logger('normal').info("打开登录页面");	
	
	// 异步读取
	fs.readFile('views/login.html', function (err, data) {
	   if (err) {
		   return console.error(err);
	   }
	   res.writeHeader(200,{"Content-Type":"text/html"});
	   res.end(data);
	});
};
//打开注册页面
exports.register = function(req, res){
  	logger.logger('normal').info("打开注册页面");
	
	// 异步读取
	fs.readFile('views/register.html', function (err, data) {
	   if (err) {
		   return console.error(err);
	   }
	   res.writeHeader(200,{"Content-Type":"text/html"});
	   res.end(data);
	});
};
//打开用户协议页面
exports.agreement = function(req, res){
  	logger.logger('normal').info("打开用户协议页面");
	
	// 异步读取
	fs.readFile('views/agreement.html', function (err, data) {
	   if (err) {
		   return console.error(err);
	   }
	   res.writeHeader(200,{"Content-Type":"text/html"});
	   res.end(data);
	});
};
//打开主页面
exports.main = function(req, res){
  	logger.logger('normal').info("打开主页面");
	
	// 异步读取
	fs.readFile('views/main.html', function (err, data) {
	   if (err) {
		   return console.error(err);
	   }
	   res.writeHeader(200,{"Content-Type":"text/html"});
	   res.end(data);
	});
};
//删除日记数据
exports.deleteNote = function(req, res){
	logger.logger('normal').info("删除日记数据");
	
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;				
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.deleteNote(datainfo.noteID,function(results){			
			res.end(results);
		});		
	});
	
};
//获取日记数据
exports.getNote = function(req, res){
	logger.logger('normal').info("获取日记数据");
	
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;		
		console.log("ok");
		
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.getNote(datainfo.userid,function(results){			
			res.end(results);
		});		
	});
	
};
//保存日记数据
exports.saveNote = function(req, res){
  	
	logger.logger('normal').info("保存日记数据");
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;		
		console.log("ok");		
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.saveNote(datainfo.noteID,datainfo.content,datainfo.userid,function(results){			
			res.end(results);
		});		
	});
};
//判断用户是否存在
exports.useridIsExist = function(req, res){
  	logger.logger('normal').info("判断用户是否存在");
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;		
		console.log("ok");
		
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.useridIsExist(datainfo.userid,function(results){			
			res.end(results);
		});		
	});
};
//登录验证
exports.login = function(req, res){
  	logger.logger('normal').info("登录验证");
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;		
		console.log("ok");
		
	});
	
	req.on('end',function(){
		console.log(postData);
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.login(datainfo.userid,datainfo.password,function(results){			
			res.end(results);
		});		
	});
};


//注册
exports.addUser = function(req,res){
	logger.logger('normal').info("注册");
	var postData = "";
	req.on('data',function(chunck){
		postData += chunck;		
		
	});
	
	req.on('end',function(){
		var dms = new DoMysql();
		var datainfo = qs.parse(postData);
		dms.addUser(datainfo.userid,datainfo.password,function(results){
			res.end(results);
		});		
	});
	
};
//打开找回密码页面
exports.forgetPassword = function(req, res){
  	logger.logger('normal').info("打开找回密码页面");
	
	// 异步读取
	fs.readFile('views/forgetPassword.html', function (err, data) {
	   if (err) {
		   return console.error(err);
	   }
	   res.writeHeader(200,{"Content-Type":"text/html"});
	   res.end(data);
	});
};